package com.castudio.Controller;

import com.alibaba.fastjson.JSONObject;
import com.castudio.Service.WechatService;
import com.castudio.Util.ApiResult;
import com.castudio.Util.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @PACKAGE_NAME: com.castudio.Controller
 * @NAME: WechatMessage
 * @USER: ASUS
 * @DATE: 2023/4/11
 * @TIME: 下午 04:18
 **/
@Slf4j
@RestController
@RequestMapping("/service")
public class WechatMessageController {
    @Autowired
    private WechatService wechatService;
    @Value("${wechat.template_1}")
    private String template1;
    @Value("${wechat.template_2}")
    private String template2;

    /**
     * 根据微信小程序开发文档
     * 1.要发送订阅信息，首先要获得用户的唯一凭证和access_code
     * 2.获得两个东西后，在获取到模板ID
     * 3.下发权限
     * 4.调用接口下法订阅信息
     * 5. 参考：https://blog.csdn.net/weixin_45031612/article/details/108129819
     *         https://blog.csdn.net/weixin_44723496/article/details/108176676
     * */
    @RequestMapping("/getOpenId")
    public String getOpenId(HttpServletRequest req){
        String js_code = req.getParameter("js_code");
        if (js_code == null || CheckUtils.isEmptyStr(js_code)) {
            return ApiResult.args();
        }
        try {
            String openid = wechatService.getOpenid(js_code);
            return ApiResult.ok(openid);
        }catch (Exception e) {
            log.error(e.toString());
            return ApiResult.error();
        }
    }
    /**
     * 当维修人员接单
     * 订阅信息
     * */
    @RequestMapping("/SendWechatMsgAccept")
    public String sendWechatMsg(HttpServletRequest req){
        String openid = req.getParameter("openid");
        String realname = req.getParameter("realname");
        String phone = req.getParameter("phone");
        String state = req.getParameter("state");
        String updatatime = req.getParameter("updatetime");
        if (openid == null || realname == null){
            return ApiResult.args();
        }
        if (openid.equals("undefined") || realname.equals("undefined"))
            return ApiResult.undefined();
        try {
            JSONObject data = new JSONObject();
            JSONObject name3 = new JSONObject();
            JSONObject phone_number5 = new JSONObject();
            JSONObject data4 = new JSONObject();
            JSONObject thing2 = new JSONObject();
            /*赋值*/
            name3.put("value",realname);
            phone_number5.put("value",phone);
            data4.put("value",state);
            thing2.put("value",updatatime);

            data.put("name3",name3);
            data.put("phone_number5",phone_number5);
            data.put("data4",data4);
            data.put("thing2",thing2);

            HashMap<String, String > result = wechatService.SendWechat(template1,openid,data,null);
            if (result.get("result").equals("success")){
                return ApiResult.ok("success");
            }
            return ApiResult.ok(result.get("result"));
        }catch (Exception e) {
            log.error(e.toString());
            return ApiResult.error();
        }
    }
    /**
     * 用户下单
     * */
    @RequestMapping("SendWechatMsgRemind")
    public String sendWechatMsgRemind(HttpServletRequest req) {
        String openid = req.getParameter("openid");
        String realname = req.getParameter("realname");
        String phone = req.getParameter("phone");
        String reason = req.getParameter("reason");
        String computername = req.getParameter("computername");
        String createtime = req.getParameter("createtime");
        if (openid == null || realname == null)
            return ApiResult.args();
        if (openid.equals("undefined") || realname.equals("undefined"))
            return ApiResult.undefined();
        try {
            JSONObject data = new JSONObject();
            JSONObject thing2 = new JSONObject();
            JSONObject phone_number3 = new JSONObject();
            JSONObject thing4 = new JSONObject();
            JSONObject thing5 = new JSONObject();
            JSONObject time8 = new JSONObject();

            /*赋值*/
            thing2.put("value", realname);
            phone_number3.put("value", phone);
            thing4.put("value", reason);
            thing5.put("value", computername);
            time8.put("value", createtime);

            data.put("thing2", thing2);
            data.put("phone_number3", phone_number3);
            data.put("thing4", thing4);
            data.put("ting5", thing5);
            data.put("time8", time8);

            HashMap<String, String> result = wechatService.SendWechatR(template2, openid, data, null);
            if (result.get("result").equals("success")) {
                return ApiResult.ok("success");
            }
            return ApiResult.ok(result.get("result"));
        } catch (Exception e) {
            log.error(e.toString());
            return ApiResult.error();
        }
    }

}
